package com.ph.util.pagination;

import java.io.Serializable;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author frank
 * @version 1.0
 */

public class PageInfo implements Serializable{

   private int totalPage; //�ܵ�ҳ��
   private int resultCount; //��Ϣ�����
   private int currentPage; //  ��ǰҳ��
   private int previousPage; //ǰһҳ��
   private int nextPage; //��һҳ��
   private int pageSize; //ҳ���С
   private int startIndex; //ҳ�濪ʼindex
   private int endIndex; //ҳ�����index

   public PageInfo(int aStartIndex, int aPageSize, int aCount) {
    //ֻ��Ҫ֪�� ҳ�濪ʼ����ҳ���С���ܼ�¼��Ŀ   �Ϳ��Լ��������һϵ�����
       int endIndex = 1;
       int currentPage = 1;

       if (aCount < (aStartIndex + aPageSize)) {
           endIndex = aCount-1;    //��ҳ�������һ��ҳ��
       } else {
           endIndex = aStartIndex + aPageSize - 1;  //��ҳ�����м�ĳ��ҳ��
       }

     
       currentPage = aStartIndex/aPageSize+1;   //��ǰҳ��

       this.setStartIndex(aStartIndex);
       this.setPageSize(aPageSize);
       this.setResultCount(aCount);

       this.setEndIndex(endIndex);
       this.setCurrentPage(currentPage);

       if (aCount % aPageSize == 0) {
           this.setTotalPage(aCount / aPageSize);
       } else {
           this.setTotalPage(aCount / aPageSize + 1);
       }

   }

   public void setTotalPage(int totalPage) {
       this.totalPage = totalPage;
   }

   public int getTotalPage() {
       return totalPage;
   }

//-------------------------------------------
   public void setResultCount(int count) {
       this.resultCount = count;
   }

   public int getResultCount() {
       return resultCount;
   }

//-------------------------------------------
   public void setCurrentPage(int currentPage) {
       this.currentPage = currentPage;
   }

   public int getCurrentPage() {
       return currentPage;
   }

//-------------------------------------------
   public void setPreviousPage(int page) {
       this.previousPage = page;
   }

   public int getPreviousPage() {
       return (this.currentPage - 1);
   }

//-------------------------------------------
   public void setNextPage(int page) {
       this.nextPage = page;
   }

   public int getNextPage() {
       return (this.currentPage + 1);
   }

   //-------------------------------------------
   public void setPageSize(int pageSize) {
       this.pageSize = pageSize;
   }

   public int getPageSize() {
       return pageSize;
   }

   //-------------------------------------------
   public void setStartIndex(int startIndex) {
       this.startIndex = startIndex;
   }

   public int getStartIndex() {
       return startIndex;
   }

   //-------------------------------------------
   public int getEndIndex() {
       return endIndex;
   }

   public void setEndIndex(int endIndex) {
       this.endIndex = endIndex;
   }

  
   public int getLastPage() {
       return totalPage ;
   }

     public int getFirstPage() {
       return 1;
   }

   
   public boolean isPrevPageExist() {
       if (startIndex <= pageSize) {
           return false;
       }
       return true;
   }

   
   public boolean isNextPageExist() {
       if (startIndex + pageSize > resultCount-1) {
           return false;
       }
       return true;
   }

  
   public int getNextPageIndex() {
       return startIndex + pageSize;
   }

   
   public int getPreviousPageIndex() {
       return startIndex - pageSize;
   }

   
   public int getPageIndex(int i) {
       return pageSize * (i - 1) + 1;
   }

}
